﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Microsoft.MediaCenter.UI;
using System.Diagnostics;
using MyVideoViewerLibrary;

namespace MyVideoViewerMCPlugin
{
    public class ActorsGallery : GalleryPage
    {
        private TVActor currentActor;
        public TVActor CurrentActor
        {
            get { return currentActor; }
            set
            {
                if (currentActor != value)
                {
                    currentActor = value;
                    FirePropertyChanged("CurrentActor");
                }
            }
        }

        public Series Series { get; set; }
        public List<TVActor> Actors { get; set; }

        public Image FanartImage
        {
            get
            {
                string imageLocation = Path.Combine(Series.FolderLocation, "fanart.jpg");
                if (File.Exists(imageLocation))
                    return new Image("file://" + imageLocation);
                else
                    return new Image("resx://MyVideoViewerMCPlugin/MyVideoViewerMCPlugin.Resources/clear");
            }
        }

        public ActorsGallery()
        { }

        public ActorsGallery(Series series)
        {
            ItemSize = GalleryItemSize.Large;
            Description = series.SeriesName + " Actors";
            Actors = new List<TVActor>();


            Series = series;
            var q = series.Series_Persons.OrderBy(s => s.Person.PersonName).ToList();

            foreach (Series_Person person in q)
            {
                Actors.Add(new TVActor(person));
            }

            VirtualList galleryList = new VirtualList(this, null);
            galleryList.EnableSlowDataRequests = true;
            galleryList.RequestSlowDataHandler = new RequestSlowDataHandler(CompleteGalleryItem);
            Content = galleryList;

            CreateGalleryFilters();
        }

        private void CreateGalleryFilters()
        {
            ArrayListDataSet list = new ArrayListDataSet(this);

            // Create the unfiltered "All" filter
            ModelItem filter = new Filter(this, Resources.GroupBy_Title, 0);

            foreach (TVActor a in Actors)
                this.Content.Add(CreateGalleryItem(a, "", false));
        }

        private ThumbnailCommand CreateGalleryItem(TVActor actorData, string description, bool isHeading)
        {
            ThumbnailCommand item = new ThumbnailCommand(this);
            item.Description = actorData.ActorName;
            item.Data = actorData;
            item.ShowHeading = isHeading;
            //item.ItemId = (int)movieData["Movie_ID"];

            // Display the release year as this item's metadata.
            //DateTime releaseDate = movieData.ReleaseDate;
            item.Metadata = description;

            item.Invoked += delegate(object sender, EventArgs args)
            {
                
            };

            item.OnFocus += delegate(object sender, EventArgs args)
            {
                ThumbnailCommand galleryItem = (ThumbnailCommand)sender;

                ActorsGallery page = (ActorsGallery)galleryItem.Owner;
                page.CurrentActor = (TVActor)galleryItem.Data;
            };

            return item;
        }

        public override int JumpTo(string searchText)
        {
            searchText = searchText.ToLower();
            List<TVActor> temp = null;
            do
            {

                temp = (from m in Actors
                        where m.ActorName.ToLower().StartsWith(searchText)
                        select m).ToList();
                if (searchText.Length > 0)
                    searchText = searchText.Substring(0, searchText.Length - 1);
            } while (temp.Count == 0 && searchText.Length > 0);

            if (temp.Count > 0)
            {
                int i = Actors.IndexOf(temp.First());
                return i;
            }
            return -1;
        }

        #region slowdatahandlers
        /// <summary>
        /// Finishes any slow data for a gallery item.
        /// </summary>
        protected void CompleteGalleryItem(VirtualList list, int index)
        {
            ThumbnailCommand item = (ThumbnailCommand)list[index];
            CompleteGalleryItem(item, (TVActor)item.Data);
        }

        /// <summary>
        /// Finishes any slow data for a gallery item.
        /// </summary>
        protected void CompleteGalleryItem(ThumbnailCommand item, TVActor actorData)
        {
            string imageName = "file://" + actorData.SeriesPerson.ImageLocation;
            item.Image = LoadImage(imageName);
        }
        #endregion
    }
}
